
//
//  PoewerNewsViewController.m
//  muscleLife
//
//  Created by xalo on 16/5/3.
//  Copyright © 2016年 HeavenAndHell. All rights reserved.
//

#import "PoewerNewsViewController.h"
#import "PowerArticleCell.h"
#import "PowerDetailViewController.h"

#define kPoewerNewsUrl @"http://health.mackentan.com/v1/article/list.do"

@interface PoewerNewsViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, retain)NSMutableArray* sourceData;
@property (nonatomic, retain)NSMutableDictionary* parameters;
@property (nonatomic, retain)NSNumber* since;

@end



@implementation PoewerNewsViewController

-(NSMutableArray *)sourceData{

    if (!_sourceData) {
        _sourceData = [NSMutableArray array];
    }
    return _sourceData;
}

-(NSMutableDictionary *)parameters{

    if (!_parameters) {
        _parameters = [NSMutableDictionary dictionary];
    }
    return _parameters;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.tableView registerNib:[UINib nibWithNibName:@"PowerArticleCell" bundle:nil] forCellReuseIdentifier:@"PowerArticleCell"];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self fetchData];
    [self setrefresh];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}

#pragma mark - <UItableViewDelegate ,UItableViewDataSource>
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    PowerArticleCell* cell = [tableView dequeueReusableCellWithIdentifier:@"PowerArticleCell" forIndexPath:indexPath];
    [cell setCellWithModel:self.sourceData[indexPath.row]];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [tableView showMessage:@"加载中..." byDataSourceCount:self.sourceData.count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    PowerDetailViewController* detialView = [[UIStoryboard storyboardWithName:@"Power" bundle:nil] instantiateViewControllerWithIdentifier:@"PowerDetailViewController"];

    detialView.model = self.sourceData[indexPath.row];
    [self.navigationController pushViewController:detialView animated:YES];

}

-(void)fetchData{
//    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [self getRequestDataWithUrl:kPoewerNewsUrl parameters:self.parameters finished:^(NSDictionary *dictionary) {

        NSLog(@"健身数据请求成功");
        NSNumber* sinceNumber = [[dictionary objectForKey:@"data"] objectForKey:@"since"];
        self.since = sinceNumber;
        NSArray* array = [[dictionary objectForKey:@"data"] objectForKey:@"articles"];
        for (int i = 0; i < array.count; i++) {
            PowerArtcles* model = [[PowerArtcles alloc] init];
            [model setValuesForKeysWithDictionary:array[i]];
            [self.sourceData addObject:model];
        }
        [self.tableView reloadData];
//        [MBProgressHUD hideHUDForView:self.tableView animated:YES];
    } failure:^(NSError *error) {
        NSLog(@"健身数据请求失败");
//        [MBProgressHUD hideHUDForView:self.tableView animated:YES];

    }];
}

-(void)refreshwithDict:(NSDictionary*)parater{

    [self getRequestDataWithUrl:kPoewerNewsUrl parameters:parater finished:^(NSDictionary *dictionary) {

        NSLog(@"刷新数据请求成功");
        NSNumber* sinceNumber = [[dictionary objectForKey:@"data"] objectForKey:@"since"];
        self.since = sinceNumber;
        NSArray* array = [[dictionary objectForKey:@"data"] objectForKey:@"articles"];
        for (int i = 0; i < array.count; i++) {
            PowerArtcles* model = [[PowerArtcles alloc] init];
            [model setValuesForKeysWithDictionary:array[i]];
            [self.sourceData addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        NSLog(@"刷新数据请求失败");
    }];
}

-(void)setrefresh{

    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreshwithDict:self.parameters];
    }];



    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self.parameters setValue:self.since forKey:@"since"];
        [self refreshwithDict:self.parameters];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
